package com.easysoft.framework.article.service;

import com.easysoft.commons.utils.DateTimeUtils;
import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.article.mapper.SysArticleCategoryMapperExtd;
import com.easysoft.framework.article.mapper.SysArticleMapperExtd;
import com.easysoft.framework.article.model.SysArticleCategory;
import com.easysoft.framework.article.model.SysArticleCategoryQuery;
import com.easysoft.framework.article.model.SysArticleCategoryView;
import com.easysoft.framework.article.model.SysArticleView;
import com.easysoft.framework.beans.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by try on 2017/7/6.
 */
@Service("service.ArticleCategoryService")
public class ArticleCategoryService {

    @Autowired
    @Qualifier("mapper.SysArticleCategoryMapperExtd")
    private SysArticleCategoryMapperExtd sysArticleCategoryMapperExtd;

    /**
     * 通过主键获取文章分类
     * @param id 主键
     */
    @DataSource(DataSource.SLAVE)
    public SysArticleCategory selectSysArticleCategoryByPrimaryKey(String id){
        return sysArticleCategoryMapperExtd.selectByPrimaryKey(id);
    }

    /**
     * 获取文章列表
     * @param page
     * @param query
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysArticleCategoryView> selectSysArticleCategoryListByPage(Page page, SysArticleCategoryQuery query){
        int maxRow = sysArticleCategoryMapperExtd.selectSysArticleCategoryListCount(query);
        page.setMaxRow(maxRow);

        if (maxRow == 0) {
            return new ArrayList<SysArticleCategoryView>();
        }
        return  sysArticleCategoryMapperExtd.selectSysArticleCategoryListByPage(page,query);
    }

    /**
     * 添加文章
     * @param sysArticleCategory
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int addSysArticleCategory(SysArticleCategory sysArticleCategory){
        sysArticleCategory.setId(UUIDUtils.getUUID());
        sysArticleCategory.setCreateTime(DateTimeUtils.getCurrentDate());
        return sysArticleCategoryMapperExtd.insertSelective(sysArticleCategory);
    }

    /**
     * 修改文章分类
     * @param sysArticleCategory
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int updateArticleCategory(SysArticleCategory sysArticleCategory){
        return sysArticleCategoryMapperExtd.updateByPrimaryKeySelective(sysArticleCategory);
    }


    /**
     * 获取所有分类
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysArticleCategoryView> selectAllCategory(){
        return sysArticleCategoryMapperExtd.selectAllCategory();
    }

    @DataSource(DataSource.MASTER)
    public int deleteArticleCategory(String id){
        return sysArticleCategoryMapperExtd.deleteByPrimaryKey(id);
    }
}
